﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;
using OgilvyOne.BLL;
using OgilvyOne.Model;

public partial class checkurl : FrontBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.QueryString["co_site"] != null)
            {
                if(!isLogin()){//本地未登录
                    if (Request.QueryString["co_site"] == "haoshiku")
                    {//好食库

                        //string resultxml = httpSend("http://cofco2011.ogilvy.com.cn/cn/member/api/islogin.aspx", "AppKey=key-hsk201110");
                        try
                        {
                            string id = Request.QueryString["id"];
                            if (!string.IsNullOrEmpty(id))
                            {
                                XmlDocument logindoc = new XmlDocument();
                                logindoc.Load("http://www.haoshiku.com/index.php?app=home&mod=Api&act=getUserInfo&appkey=68a8a23143fb4a4ee11117bbf2396cd3&id=" + id);//"http://cofco2011.ogilvy.com.cn/cn/member/api/islogin.aspx");
                                XmlNode cofco = logindoc.SelectSingleNode("//cofco");
                                if (cofco != null)
                                {
                                    XmlNode type = cofco.SelectSingleNode("//result");
                                    if (type != null)
                                    {
                                        string tv = type.Attributes["type"].Value.Trim();
                                        if (tv == "0")
                                        {//已登录
                                            XmlNode user = cofco.SelectSingleNode("//user");
                                            //string loginid = user.SelectSingleNode("//loginId").InnerText;
                                            string username = user.SelectSingleNode("//username").InnerText;
                                            string nickname = user.SelectSingleNode("//usernickname").InnerText;
                                            MemberInfoBll mbll = new MemberInfoBll();
                                            XmlNode xtype = user.SelectSingleNode("//type");
                                            string usertype = MemberInfoBll.MEMBER_TYPE_HSK;
                                            if (xtype != null)
                                            {
                                                if (xtype.InnerText.ToLower() == "cofco")
                                                {
                                                    usertype = MemberInfoBll.MEMBER_TYPE_COFCO;
                                                }
                                            }
                                            MemberInfoModel m = mbll.GetMemberInfoModel(username, usertype);
                                            if (m == null)
                                            {//不存在
                                                m = new MemberInfoModel();
                                                m.UserType = MemberInfoBll.MEMBER_TYPE_HSK;
                                                m.Username = username;
                                                m.Password = "";
                                                m.MemberNickname = nickname;
                                                mbll.Reg(m);
                                            }
                                            else
                                            {//存在
                                                mbll.SetLoginValue(m);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                }
                if (Request.QueryString["url"] != null)
                {
                    string url = Request.QueryString["url"];
                    Response.Redirect(url);
                }
            }
        }
    }

    public string httpSend(string url, string formatString)
    {
        CookieContainer cookies = new CookieContainer();
        string curl = url;
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(curl);
        myHttpWebRequest.Timeout = 20 * 1000; //连接超时
        myHttpWebRequest.Accept = "*/*";
        myHttpWebRequest.UserAgent = "www.cofco.com";
        myHttpWebRequest.Headers.Add("Referer", "www.cofco.com");
        myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例
        try
        {
            myHttpWebRequest.GetResponse().Close();
        }
        catch { }
        cookies = myHttpWebRequest.CookieContainer; //保存cookies
        string cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); //把cookies转换成字符串

        string responseString = string.Empty;
        ASCIIEncoding encoding = new ASCIIEncoding();
        HttpWebResponse response;
        //byte[] data = encoding.GetBytes("cofco");
        StreamReader reader;
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url + "?" + formatString);

        //do the processing
        SetRequestProperties(request, "GET");  // SETTING METHOD TO GET HERE
        request.CookieContainer = cookies; 
        response = (HttpWebResponse)request.GetResponse();
        reader = new StreamReader(response.GetResponseStream());
        responseString = reader.ReadToEnd();

        //clean up
        response.Close();
        response.GetResponseStream().Close();
        response.GetResponseStream().Dispose();
        reader.Close();
        reader.Dispose();
        reader = null;
        response = null;
        request = null;
        encoding = null;

        return responseString;
    }

    private static void SetRequestProperties(HttpWebRequest request, string s)
    {
        request.Method = s;
        request.AllowWriteStreamBuffering = true;
        request.KeepAlive = false;
        request.ContentType = "application/x-www-form-urlencoded";
        request.SendChunked = false;
        request.Credentials = CredentialCache.DefaultCredentials;
        request.UserAgent = "www.cofco.com";
        request.Timeout = 60000;
        request.ProtocolVersion = new System.Version("1.1");
    }

}
